home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / GETDCWD.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  2KB  |  75 lines

  1. /*
  2. ** GETDCWD.C - returns the current working directory for a specific drive
  3. **
  4. ** public domain by Bob Jarvis, modified by Bob Stout
  5. */
  6.  
  7. #if defined(__ZTC__)
  8.  #define GetDrive(d) dos_getdrive(&d)
  9.  #define FAR _far
  10. #elif defined(__TURBOC__)
  11.  #define GetDrive(d) ((d) = getdisk() + 1)
  12.  #define FAR far
  13. #else /* assume MSC */
  14.  #define GetDrive(d) _dos_getdrive(&d)
  15.  #define FAR _far
  16. #endif
  17.  
  18. #include <dos.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22.  
  23. char *getdcwd(unsigned int drive)   /* 0 = current, 1 = A, 2 = B, etc */
  24. {
  25.       union REGS regs;
  26.       struct SREGS sregs;
  27.       char *retptr;
  28.  
  29.       retptr = calloc(FILENAME_MAX + 4, sizeof(char));
  30.       if(retptr == NULL)
  31.             return NULL;
  32.  
  33.       if(drive == 0)    /* figure out which drive is current */
  34.       {
  35.             GetDrive(drive);
  36.             drive += 1;
  37.       }
  38.  
  39.       *retptr = (char)((drive-1) + 'A');
  40.       *(retptr+1) = ':';
  41.       *(retptr+2) = '\\';
  42.  
  43.       segread(&sregs);
  44.       regs.h.ah = 0x47;
  45.       regs.h.dl = (unsigned char)drive;
  46.       sregs.ds  = FP_SEG((void FAR *)retptr);
  47.       regs.x.si = FP_OFF((void FAR *)retptr) + 3;
  48.  
  49.       intdosx(®s, ®s, &sregs);
  50.       if (15 == regs.x.ax)     /* drive number invalid */
  51.       {
  52.             free(retptr);
  53.             return NULL;
  54.       }
  55.       else  return retptr;
  56. }
  57.  
  58. #ifdef TEST
  59.  
  60. void main(int argc, char *argv[])
  61. {
  62.       char *curpath;
  63.       unsigned int n;
  64.  
  65.       if(argc > 1)
  66.             n = (tolower(*argv[1]) - 'a') + 1;
  67.       else  GetDrive(n);
  68.       
  69.       printf("curpath = '%s'\n", curpath = getdcwd(n));
  70.       if (curpath)
  71.             free(curpath);
  72. }
  73.  
  74. #endif
  75.